﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace JetLoginTool.Engine
{
	public class Location
	{
		#region Fields
		private static readonly FileInfo locationsFile = new FileInfo(string.Format("{0}\\locations.xml", Application.StartupPath));
		#endregion
		#region Constructors
		public Location()
		{
			Name = "";
			InternalUrl = null;
			JetLoginUrl = null;
			JetPopupTextUrl = null;
		}
		
		public Location(Location inLocation)
		{
			InternalUrl = inLocation.InternalUrl;
			JetLoginUrl = inLocation.JetLoginUrl;
			JetPopupTextUrl = inLocation.JetPopupTextUrl;
		}
		#endregion
		#region Properties
		/// <summary>
		/// Name for this location
		/// Eg. Curtin University (Bentley)
		/// </summary>
		public string Name { get; set; }

		/// <summary>
		/// A URL inside the network that can be accessed without Jet Authentication
		/// Eg. http://www.curtin.edu.au
		/// </summary>
		[XmlIgnore]
		public Uri InternalUrl { get; set; }


		/// <summary>
		/// The login page for jet (where username\password are enterred)
		/// Eg. https://jetib.curtin.edu.au/curtin/login/portal
		/// </summary>
		[XmlIgnore]
		public Uri JetLoginUrl { get; set; }

		/// <summary>
		/// The page that shows the user's statistics (NOT the page with the pretty graph)
		/// Eg. https://jetib.curtin.edu.au/curtin/portal/popup_text_refresh
		/// </summary>
		[XmlIgnore]
		public Uri JetPopupTextUrl { get; set; }

		/// <summary>
		/// The page that logs out the user (NOTE: need to clear cookies before hand)
		/// Eg. https://jetib.curtin.edu.au/curtin/portal/logout
		/// </summary>
		[XmlIgnore]
		public Uri LogoutUrl { get; set; }

		/// <summary>
		/// A URL inside the network that can be accessed without Jet Authentication
		/// Eg. http://www.curtin.edu.au
		/// </summary>
		public string InternalUrlString 
		{
			get
			{ return InternalUrl.ToString(); }
			set
			{ InternalUrl = new Uri(value); }
		}

		/// <summary>
		/// The login page for jet (where username\password are enterred)
		/// Eg. https://jetib.curtin.edu.au/curtin/login/portal
		/// </summary>
		public string JetLoginUrlString
		{
			get
			{ return JetLoginUrl.ToString(); }
			set
			{ JetLoginUrl = new Uri(value); }
		}

		/// <summary>
		/// The page that shows the user's statistics (NOT the page with the pretty graph)
		/// Eg. https://jetib.curtin.edu.au/curtin/portal/popup_text_refresh
		/// </summary>
		public string JetPopupTextUrlString
		{
			get
			{ return JetPopupTextUrl.ToString(); }
			set
			{ JetPopupTextUrl = new Uri(value); }
		}

		/// <summary>
		/// The page that logs out the user (NOTE: need to clear cookies before hand)
		/// Eg. https://jetib.curtin.edu.au/curtin/portal/logout
		/// </summary>
		public string LogoutUrlString
		{
			get
			{ return LogoutUrl.ToString(); }
			set
			{ LogoutUrl = new Uri(value); }
		}
		#endregion
		#region Methods
		public static List<Location> loadLocations()
		{
			List<Location> output = new List<Location>(10);
			XmlSerializer engineSerialize = new XmlSerializer(typeof(List<Location>));

			try
			{
				using (Stream locationsStream = locationsFile.OpenRead())
				{
					output = (List<Location>)engineSerialize.Deserialize(locationsStream);
				}
			}
			catch (SystemException ex)
			{
				throw new LocationsInvalidException("Error loading locations.xml file", ex);
			}


			if (output.Count == 0)
			{
				throw new LocationsInvalidException("No locations contained in locations.xml file");
			}

			return output;
		}
		#endregion
	}

	[Serializable]
	public class LocationsInvalidException : SystemException
	{
		public LocationsInvalidException(string message, Exception inInnerException)
			: base(message, inInnerException)
		{ }

		public LocationsInvalidException(string message)
			: base(message)
		{ }
	}
}